mod routes;
mod controller;
mod dao;
mod service;
mod middleware;
mod config;
mod model;
mod utils;

use actix_web::{App, HttpServer, web, middleware::Logger};
use env_logger::Env;
use crate::{config::{config::{init_config, Settings}, db::sql_connect}, middleware::state::AppState};
use routes::init_routes;


#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
    let settings: Settings = init_config().try_deserialize().unwrap();
    let conn = sql_connect(settings.database).await;
    println!("http://{}", settings.server.host);
    HttpServer::new(move || App::new()
            .wrap(Logger::new("host=%a request{%r} statusCode=%s resSize=%b time=%T serverTime=%D"))
            .app_data(web::Data::new(AppState { conn: conn.clone() }))
            .configure(init_routes)
        )
        .bind(&settings.server.host)?
        .run()
        .await
}
